package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.ClassRoomBean;
import com.oscar.ehome.bean.pagebean.ClassRoomPrombleBean;
import com.oscar.ehome.presenter.ClassRoomPresenter;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshGridView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.messageview.adapter.MessageListAdapter;
import com.oscar.ehome.view.pageview.adapter.ClassoomAdapter;
import com.oscar.ehome.view.pageview.view.ClassRoomView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/15
 * 小课堂页面
 */

public class ClassroomActivity extends BasesActivity<ClassRoomView, ClassRoomPresenter> implements AdapterView.OnItemClickListener, ClassRoomView,
        ProgressFrameLayout.AgainRequestListener {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 筛选
     */
    @BindView(R.id.home_tv_num)
    TextView homeTvNum;
    /**
     * 筛选
     */
    @BindView(R.id.right_layout)
    RelativeLayout rightLayout;
    /**
     * 布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * grid view
     */
    @BindView(R.id.classroom_gridview)
    GridView classroomGridview;
    /**
     * 无数据布局
     */
    @BindView(R.id.classroom_fragment)
    ProgressFrameLayout classroomFragment;

    private String token = "";

    ClassoomAdapter adapter;
    ClassRoomPresenter presenter;

    private List<ClassRoomBean.ClassRoomListBean> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classroom);
        ButterKnife.bind(this);
        setColor();
        initView();
        onCLicks();
        initData();
    }

    @Override
    protected ClassRoomPresenter createPresenter() {
        presenter = new ClassRoomPresenter(this);
        return presenter;
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(ClassroomActivity.this).getSystemConfig().getColor()));
    }

    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleTopbar.setText("小课堂");
        token = SystemConfigFactory.getInstance(ClassroomActivity.this).getSystemConfig().getToken();

        adapter = new ClassoomAdapter(ClassroomActivity.this, dataList);
        classroomGridview.setAdapter(adapter);
    }

    public void onCLicks() {
        classroomGridview.setOnItemClickListener(this);
        classroomFragment.setAgainRequestListener(this);
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(ClassroomActivity.this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    public void initData() {
        if (isNetWork()) {
            classroomFragment.stopRefresh();
            classroomGridview.setVisibility(View.VISIBLE);
            presenter.getKnowList(token, "1", "20");
            showLoading();
        } else {
            classroomFragment.setVisibility(View.VISIBLE);
            classroomFragment.setLoadFail();
            classroomGridview.setVisibility(View.GONE);
        }
    }


    @OnClick({R.id.title_layout_left, R.id.right_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left:
                finish();
                break;
            case R.id.right_layout:

                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if (dataList.size() > 0) {
            ClassRoomBean.ClassRoomListBean bean = dataList.get(position);
            String addapp = new Gson().toJson(bean);
            Intent intent = new Intent(ClassroomActivity.this, ClassRoomVideoActivity.class);
            intent.putExtra("messagebean", addapp);
            intent.putExtra("lawyer", 1);
            startActivity(intent);
        }

    }

    @Override
    public void knowledgeList(ClassRoomBean result) {
        dismissLoading();
        if (result != null) {
            dataList = new ArrayList<>();
            dataList = result.getList();
            if (dataList.size() > 0) {
                classroomGridview.setVisibility(View.VISIBLE);
                adapter.setList(dataList);
            } else {
                classroomGridview.setVisibility(View.GONE);
                classroomFragment.setNoResult();
            }
        }
    }

    @Override
    public void problemList(ClassRoomPrombleBean result) {

    }

    @Override
    public void sendpromble(String result) {

    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(ClassroomActivity.this, getString(R.string.erry));
        classroomFragment.setLoadFail();
        classroomGridview.setVisibility(View.GONE);
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(ClassroomActivity.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(ClassroomActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(ClassroomActivity.this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(ClassroomActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            classroomFragment.setLoadFail();
            classroomGridview.setVisibility(View.GONE);
        }
    }


    @Override
    public void againRequest() {
        if (isNetWork()) {
            initData();
        } else {
            ToastUtil.show(ClassroomActivity.this, R.string.pull_to_refresh_network_error);
        }
    }
}
